package com.example.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.Utils;
import com.example.demo.dao.IconListDaoMapper;
import com.example.demo.entity.*;
import com.example.demo.service.WeatherServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author littlebadfox
 * @date 2022-04-24-18:53
 * @desc
 */
@Service
public class WeatherService implements WeatherServiceImpl {

    @Resource
    private RestTemplate restTemplate;

    final String weatherkey="9f4f6d9ef9bb4c5faa25e174db1c9303";

    @Resource
    Utils utils;

    @Autowired
    IconListDaoMapper iconListDaoMapper;

//    返回实时的天气数据
    @Override
    public Weather getWeatherNow(String la,String lo) {
        String sla=String.format("%.2f",Double.parseDouble(la));
        String slo=String.format("%.2f",Double.parseDouble(lo));
        String location=slo+","+sla;
        Weather weather=new Weather();
        String url="https://devapi.qweather.com/v7/weather/now?location="+location+"&key="+weatherkey;
        Map<String,Object> map=restTemplate.getForObject(url,Map.class);
//        System.out.println(map);
        weather= utils.parseMap2Object((Map<String, Object>) map.get("now"),Weather.class);
//        Map<String,Object> map1= (Map<String, Object>) map.get("now");
//        weather.setObsTime((String) map1.get("obsTime"));
//        weather.setTemp((String) map1.get("temp"));
//        weather.setFeellike((String) map1.get("feelsLike"));
//        weather.setIcon((String) map1.get("icon"));
//        weather.setText((String) map1.get("text"));
//        weather.setWind360((String) map1.get("wind360"));
//        weather.setWindDir((String) map1.get("windDir"));
//        weather.setWindScale((String) map1.get("windScale"));
//        weather.setWindSpeed((String) map1.get("windSpeed"));
//        weather.setHumidity((String) map1.get("humidity"));
//        weather.setPrecip((String) map1.get("precip"));
//        weather.setPressure((String) map1.get("pressure"));
//        weather.setVis((String) map1.get("vis"));
//        weather.setCloud((String) map1.get("cloud"));
//        weather.setDew((String) map1.get("dew"));
//        System.out.println(map1);
//        System.out.println(weather.getObsTime()+"   "+map1.get("obsTime"));
//          weather=JSON.parseObject(str,Weather.class);
//          System.out.println(weather);
        return weather;
    }

    //未来三天的天气数据
    @Override
    public WeatherDaily getWeather3D(String la,String lo) {
        String sla=String.format("%.2f",Double.parseDouble(la));
        String slo=String.format("%.2f",Double.parseDouble(lo));
        String location=slo+","+sla;
//        System.out.println(location);
        WeatherDaily weatherDaily=new WeatherDaily();
        String url="https://devapi.qweather.com/v7/weather/3d?location="+location+"&key="+weatherkey;
        Map<String,Object> map=restTemplate.getForObject(url,Map.class);
//        System.out.println(map);
        List<Daily>  dailies=utils.parseMaplist2ObjectList((List<Object>) map.get("daily"),Daily.class);
        weatherDaily.setUpdateTime((String) map.get("updateTime"));
        weatherDaily.setDailyList(dailies);
        return weatherDaily;
    }

    @Override
    public String getNowDate() {
        Date date=new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd EEEE");
//        System.out.println(formatter.format(date));
        return formatter.format(date);
    }

    //获取天气指数信息
    @Override
    public List<WeatherIndex> getWeatherIndexIndo(String la, String lo) {
        String sla=String.format("%.2f",Double.parseDouble(la));
        String slo=String.format("%.2f",Double.parseDouble(lo));
        String location=slo+","+sla;
        String url="https://devapi.qweather.com/v7/indices/1d?type=1,3,8,9,15&location="+location+"&key="+weatherkey;
        Map<String,Object> map=restTemplate.getForObject(url,Map.class);
        List<WeatherIndex> list=utils.parseMaplist2ObjectList((List<Object>) map.get("daily"),WeatherIndex.class);
        List<String> list1=iconListDaoMapper.getIndexOfUrl();
        int i=0;
        for(WeatherIndex w:list){
            w.setImageUrl(list1.get(i++));
        }
//        System.out.println(list);
        return list;
    }

    @Override
    public List<Object> getSerachList(String location) {
        String url="https://geoapi.qweather.com/v2/city/lookup?location="+location+"&range=cn&key="+weatherkey;
        Map<String,Object> map=restTemplate.getForObject(url,Map.class);
        if(map.get("code").equals("400")){
            return null;
        }
//        System.out.println(map);
        List<Object> objects= (List<Object>) map.get("location");
        return objects;
    }
//   未来七天的天气
    @Override
    public ForeWeatherInfo getWeather7D(String la, String lo) throws ParseException {
        String sla=String.format("%.2f",Double.parseDouble(la));
        String slo=String.format("%.2f",Double.parseDouble(lo));
        String location=slo+","+sla;
        WeatherDaily weatherDaily=new WeatherDaily();
        String url="https://devapi.qweather.com/v7/weather/7d?location="+location+"&key="+weatherkey;
        Map<String,Object> map=restTemplate.getForObject(url,Map.class);
        List<Daily>  dailies=utils.parseMaplist2ObjectList((List<Object>) map.get("daily"),Daily.class);
        weatherDaily.setUpdateTime((String) map.get("updateTime"));
        weatherDaily.setDailyList(dailies);
        for(Daily d:dailies){
            d.setWeekdate(utils.getWeek(d.getFxDate()));
        }
        List<Daily> dailies1 = new ArrayList<>();
        for(int i=1;i<4;i++){
            dailies1.add(weatherDaily.getDailyList().get(i));
        }
        ForeWeatherInfo foreWeatherInfo=new ForeWeatherInfo();
        foreWeatherInfo.setFirst(dailies1);
        dailies1=new ArrayList<>();
        for (int i=4;i<7;i++){
            dailies1.add(weatherDaily.getDailyList().get(i));
        }
        foreWeatherInfo.setSecond(dailies1);
        return foreWeatherInfo;
    }


}
